package com.zyx.camunda.controller;

import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.batch.Batch;
import org.camunda.bpm.engine.migration.MigrationPlan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Yaxi.Zhang
 * @since 2022/10/18 00:09
 */
@RestController
@RequestMapping("/example")
public class ExampleController {

    @Autowired
    private RuntimeService runtimeService;

    @GetMapping("/migration/{instanceId}")
    public String migration(@PathVariable("instanceId") String instanceId) {
        String sourceProcessDefinitionId = "batch:1:1582012866367639552";
        String targetProcessDefinitionId = "batch:2:1582021566767656960";

        String sourceActivityId = "Activity_08hk2ft";
        String targetActivityId = "Activity_1nicj02";

        MigrationPlan migrationPlan = runtimeService
                .createMigrationPlan(sourceProcessDefinitionId, targetProcessDefinitionId)
                .mapActivities(sourceActivityId, targetActivityId)
                .build();
        // 添加异步任务，注意，此时该任务并未真实执行
        Batch batch = runtimeService.newMigration(migrationPlan)
                .processInstanceIds(instanceId)
                .executeAsync();
        return "success";
    }

}
